package com.ivan.stu.notetool.utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IVAN on 2019/2/21.
 */

public class HtmlUtil {

    /**
     * 从一段文本中找出img标签的src路径
     *
     * @param text
     * @return
     */
    public static ArrayList<String> getImageSrcFromText(String text) {
        //     Log.e("text",text);
        ArrayList<String> list = new ArrayList<>();
        Pattern img = Pattern.compile("<(img)(.*?)(/>|></img>|>)", Pattern.CASE_INSENSITIVE);
        Matcher imgMatcher = img.matcher(text);
        boolean hasImg = imgMatcher.find();
        if (hasImg) {
            while (hasImg) {
                String imgString = imgMatcher.group(2);

                Pattern src = Pattern.compile("(src)=(\"|\')(.*?)(\"|\')", Pattern.CASE_INSENSITIVE);
                Matcher srcMatcher = src.matcher(imgString);
                if (srcMatcher.find()) {
                    String srcString = srcMatcher.group(3);
                    list.add(srcString);
                }

                hasImg = imgMatcher.find();
            }
        }
        return list;
    }



    public static boolean hasText(String htmlString) {
        String regularExpression = "<[^>]+>";
        Pattern pattern = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(htmlString);
        String tmp = matcher.replaceAll("").trim();
        return tmp.length() > 0;
    }

    public static String getTextFromHtml(String htmlString){
        String regularExpression = "<[^>]+>";
        Pattern pattern = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(htmlString);
        String tmp = matcher.replaceAll("").trim();
        return tmp;
    }


    //判断是否有html标签

    public static boolean hasHtmlTag(String string) {
        String regularExpression = "<[^>]+>";
        Pattern pattern = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }

    public interface OnReplaceAttributeCallback {
        String onReplace(String attribute, String attributeValue);
    }



    //替换html中某个标签中的属性值

    public static String replaceAttribute(String html,
                                          String tag,
                                          String attribute,
                                          OnReplaceAttributeCallback callback) {
        String regexTag = "<\\s*" + tag + "\\s+([^>]*)\\s*";
        Pattern patternTag = Pattern.compile(regexTag, Pattern.CASE_INSENSITIVE);
        Matcher matcherTag = patternTag.matcher(html);

        String regexAttribute = attribute + "\\s*=\\s*([\"|\'])(.*?)([\"|\'])\\s*";
        Pattern patternAttribute = Pattern.compile(regexAttribute, Pattern.CASE_INSENSITIVE);
        String regexAttribute_ = attribute + "\\s*=\\s*(\\S+)\\s*";
        Pattern patternAttribute_ = Pattern.compile(regexAttribute_, Pattern.CASE_INSENSITIVE);

        StringBuffer sb = new StringBuffer();
        while (matcherTag.find()) {
            String input = matcherTag.group(1);

            Matcher matcherAttribute = patternAttribute.matcher(input);

            StringBuffer tmp = new StringBuffer("<" + tag + " ");
            if (matcherAttribute.find()) {
                String attributeValue = matcherAttribute.group(2);
                String replacement = callback.onReplace(attribute, attributeValue.trim()) + " ";
                matcherAttribute.appendReplacement(tmp, replacement);
            } else {
                matcherAttribute = patternAttribute_.matcher(input);

                if (matcherAttribute.find()) {
                    String attributeValue = matcherAttribute.group(1);
                    String replacement = callback.onReplace(attribute, attributeValue.trim()) + " ";
                    matcherAttribute.appendReplacement(tmp, replacement);
                }
            }

            matcherAttribute.appendTail(tmp);
            matcherTag.appendReplacement(sb, tmp.toString());
        }
        matcherTag.appendTail(sb);

        return sb.toString();
    }


    //去除某个标签中的某个属性值

    public static String removeAttribute(String html,
                                         String tag,
                                         String attribute) {
        String regexTag = "<\\s*" + tag + "\\s+([^>]*)\\s*";
        Pattern patternTag = Pattern.compile(regexTag, Pattern.CASE_INSENSITIVE);
        Matcher matcherTag = patternTag.matcher(html);

        String regexAttribute = attribute + "\\s*=\\s*(([\"|\']([^\"|\']+)[\"|\'])|(\\S+))\\s?";
        Pattern patternAttribute = Pattern.compile(regexAttribute, Pattern.CASE_INSENSITIVE);

        StringBuffer sb = new StringBuffer();
        while (matcherTag.find()) {
            String input = matcherTag.group(1);
            Matcher matcherAttribute = patternAttribute.matcher(input);

            StringBuffer tmp = new StringBuffer("<" + tag + " ");
            if (matcherAttribute.find()) {
                matcherAttribute.appendReplacement(tmp, "");
            }

            matcherAttribute.appendTail(tmp);
            matcherTag.appendReplacement(sb, tmp.toString());
        }
        matcherTag.appendTail(sb);

        return sb.toString();
    }

    //去除某个标签中的所有属性值

    public static String removeAllAttribute(String html, String tag) {
        String regexTag = "<\\s*" + tag + "\\s+([^>]*)\\s*";
        Pattern patternTag = Pattern.compile(regexTag, Pattern.CASE_INSENSITIVE);
        Matcher matcherTag = patternTag.matcher(html);

        StringBuffer sb = new StringBuffer();
        while (matcherTag.find()) {
            matcherTag.appendReplacement(sb, "<" + tag + " ");
        }
        matcherTag.appendTail(sb);
        return sb.toString();

    }

    public interface OnAddAttributeCallback {
        String onAdd(String attribute);
    }



    //给某个标签添加新的属性值

    public static String addAttribute(String html,
                                      String tag,
                                      String attribute,
                                      OnAddAttributeCallback callback) {
        String regexTag = "<\\s*" + tag;
        Pattern patternTag = Pattern.compile(regexTag, Pattern.CASE_INSENSITIVE);
        Matcher matcherTag = patternTag.matcher(html);

        StringBuffer sb = new StringBuffer();
        while (matcherTag.find()) {
            StringBuffer tmp = new StringBuffer("<" + tag + " ");
            tmp.append(callback.onAdd(attribute));
            matcherTag.appendReplacement(sb, tmp.toString());
        }
        matcherTag.appendTail(sb);

        return sb.toString();

    }

}
